package com.heima.media.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.WmSensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author Wq
 * @since 2022-10-12
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    @Override
    public ResponseResult pageByCondition(WmSensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (ObjUtil.isNull(dto.getPage()) || dto.getPage() < 0) {
            dto.setPage(1L);
        }
        if (ObjUtil.isNull(dto.getSize()) || dto.getSize() < 0) {
            dto.setSize(10);
        }
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getName()), WmSensitive::getSensitives, dto.getName())
                .orderByDesc(WmSensitive::getCreatedTime);
        Page<WmSensitive> page = this.page(new Page<>(dto.getPage().intValue(), dto.getSize()), wrapper);
        return new PageResponseResult(page.getPages(), Convert.toInt(page.getSize()), page.getTotal(), page.getRecords());
    }
}
